import {
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToOne,
  JoinColumn,
  ManyToMany,
  JoinTable
} from 'typeorm'
import { EntityModel } from '@midwayjs/orm'
import { SystemUser } from './system_user'
import { Department } from './department'
import { District } from './district'

@EntityModel('system_user_meta')
export class SystemUserMeta {
  
  @PrimaryGeneratedColumn()
  id: number;

  @OneToOne(() => SystemUser, { nullable: false })
  @JoinColumn({ name: 'system_user_id' })
  system_user: SystemUser;

  // 账号所属部门
  @ManyToOne(() => Department)
  @JoinColumn({ name: 'department_id' })
  department: Department;

  // 账号所属辖区
  @ManyToMany(() => District)
  @JoinTable({
    name: 'system_users_districts',
    joinColumn: {
      name: 'system_user_meta_id',
      referencedColumnName: 'id'
    },
    inverseJoinColumn: {
      name: 'district_id',
      referencedColumnName: 'id'
    }
  })
  districts: District[];

  @CreateDateColumn({
    comment: '创建时间'
  })
  create_at: Date;

  @UpdateDateColumn({
    comment: '更新时间'
  })
  update_at: Date;
}
